1675B - Make It Increasing - CodeForces Solution


greedy implementation *900

Please click on ads to support us..

Python Code:

for s in[*open(0)][2::2]:
 *a,=map(int,s.split());r=1;p=3e9
 while a and p:
  q=a.pop()
  while q>=p:r+=1;q>>=1
  p=q
 print(r*(a==[])-1)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define pb push_back
#define forn(i,x,n) for(int i=x; i<n; i++)

void solve() {

  ll n;
  cin>>n;
  ll arr[n];
  forn(i,0,n) cin>>arr[i];
  int cnt = 0;
  
  for(int i=n-1; i>0; i--){

    if(arr[i-1] >= arr[i]){

      if(arr[i] == 0){
        cout<<"-1\n";
        return;
      }
      while(arr[i-1] >= arr[i]){
        arr[i-1] /= 2;
        cnt++;
      }
    }
  }
  cout<<cnt<<"\n";
}

int main(){
  int t;
  cin>>t;
  while(t--){
    solve();
  }
}


Comments

Submit
0 Comments
More Questions

1036D - Vasya and Arrays
1139C - Edgy Trees
37A - Towers
353A - Domino
409H - A + B Strikes Back
1262A - Math Problem
158C - Cd and pwd commands
194A - Exams
1673B - A Perfectly Balanced String
1104B - Game with string
1169B - Pairs
1567D - Expression Evaluation Error
78A - Haiku
1287A - Angry Students
1428A - Box is Pull
234B - Reading
581B - Luxurious Houses
1481C - Fence Painting
935A - Fafa and his Company
22A - Second Order Statistics
1720B - Interesting Sum
1720A - Burenka Plays with Fractions
3A - Shortest path of the king
1720C - Corners
574A - Bear and Elections
352B - Jeff and Periods
1244A - Pens and Pencils
1670A - Prof Slim
1189A - Keanu Reeves
678A - Johny Likes Numbers